<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\App;
use util\Token;

/**
 * Purchase控制器
 */
class Purchase extends AdminBase
{

    protected $model = null;
    protected $adminId = null;
    protected $token = null;

    protected $validate = null;

    protected $relationSearch = false;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\Purchase();

        $this->validate = \app\admin\validate\Purchase::class;
        $this->token = $this->request->header('token');
        $adminId = Token::userId($this->token);
        $this->adminId = $adminId;
    }



    public function index()
    {
        //是否是超管
        $is_superadmin = Token::is_superadmin($this->token);
        if (!$is_superadmin) {
            $where[] = ['admin_id', '=', $this->adminId];
        }
        list($limit, $where, $sortArr) = $this->buildTableParames();
        $list = $this->model
            ->field("id,brand,color,sku, SUM(`num`) as num,admin_id,batch,apply_time,merge_status,merge_batch")
            ->where($where)
            ->order('apply_time desc')
            ->group('batch')
            ->paginate($limit);
        foreach ($list as $k => $v) {
            //调用合并审核的批次号 查询相应的状态
            $list[$k]['merge_status_title'] =   $this->get_merge_status($v['merge_status']);
            $list[$k]['merge_batch'] = !empty($v['merge_batch']) ? $v['merge_batch']  : '';
        }
        $data = [
            'code' => 1,
            'msg' => '',
            'count' => $list->total(),
            'data' => $list->items(),
        ];
        return json($data);
    }



    public  function  get_merge_status($id = '')
    {
        $array = [
            0 => '待合并',
            1 => '待审核',
            2   => '部分待采',
            3  => '待采购',
            4  => '部分待入库',
            5  => '待入库',
            6  => '已完成',
            7 => '审核作废',
            8 => '已驳回',
            9 => '待采购已取消',
        ];
        return isset($array[$id]) ? $array[$id] : '';
    }


    //审核详情
    public function edit()
    {
        $id = input('id');
        $row = $this->model->find($id);
        if (empty($row)) {
            return error('数据不存在');
        }
        //通过批次号
        $batch = !empty($row['batch']) ? $row['batch'] : 20230817327446;
        //未审核状态
        $list = table('purchase')
            ->field("brand,SUM(`num`) as num, color,sku,admin_id,product_id,apply_status,apply_type,batch,apply_time,merge_status")
            ->where(['batch' => $batch])
            ->order('apply_time desc')
            ->group('sku,color,batch')
            ->select()
            ->toarray();
        foreach ($list as $key => $value) {
            $color = table('color')->where(['title' => $value['color']])->find();
            $admin = table('system_admin')->where(['id' => $value['admin_id']])->find();
            $list[$key]['admin_title'] =  !empty($admin['nickname']) ? $admin['nickname'] : '';
            $list[$key]['color'] =  !empty($color) ? $color['translate'] : '';
            $list[$key]['apply_type_title'] =  $value['apply_type'] == 1 ? '采购申请' : '手动采购';
            $list[$key]['merge_status_title'] = $this->get_merge_status($value['merge_status']);
            $list[$key]['merge_sku'] = $value['color'] . '-' . $value['sku'];
        }
        return success('ok', $list);
    }


    //批次号合并操作

    public function merge_batch()
    {
        $post = $this->request->post();
        $batch_ids = !empty($post['batchs']) ? $post['batchs'] : 0;
        // if (count($batch_ids) < 2) {
        //     return error('合并数量不能少于2');
        // }
        //接收合并的id
        $where[] = ['batch', 'in', $batch_ids];
        $merge_batch  = date('Ymd') . rand(1000, 9999);
        $merge_time  = date('Y-m-d H:i:s');
        table('purchase')->where($where)->update(['merge_batch' => $merge_batch, 'merge_time' => $merge_time, 'merge_status' => 1]);
        return success('操作成功');
    }
}
